﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waiter.Common;
using Waiter.Controler;
using DevComponents.DotNetBar.Controls;
using DevComponents.DotNetBar;
using Waiter.DataLayer;

namespace Waiter.View
{
    public partial class frmSalary : DevComponents.DotNetBar.Office2007Form//Form
    {
        ActionStatus myActionStatus = new ActionStatus();       //Trạng thái hiện tại của form
        StaffCtrl myStaffCtrl = new StaffCtrl();
        SalaryCtrl mySalaryCtrl = new SalaryCtrl();
        BindingSource myBinding = new BindingSource();
        public delegate void CloseForm();                       //Delegate close form
        public CloseForm myCloseForm;                           //Tạo thể hiện của delegate

        public frmSalary()
        {
            InitializeComponent();
        }

        #region Events ******************************************

        /// <summary>
        /// Form load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmSalary_Load(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmSalary.frmSalary_Load", "", "");
            //Load trị mặc định vào các control 
            dateTime.Value = DateTime.Now;
            myStaffCtrl.fsLoadComboBox_ListStaff(cmbStaff);
            myStaffCtrl.fsLoadComboBox_ListStaff(cmbStaffBottom);
            datePayDate.Value = DateTime.Now;
            numSalaryChange.Value = 0;
            myActionStatus = ActionStatus.DEFAULT;
            cmbFirstOrEnd.SelectedIndex = 0;
            lbSumSalary.Text = "";
        }

        /// <summary>
        /// Ấn button thay đổi lương
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEdit_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmSalary.btnEdit_Click", "", "");
            if (grdMain.CurrentCell != null)
            {
                //pnlBottom.Visible = true;
                //pnlTop.Enabled = false;
                //pnlCenter.Enabled = false;
                this.fsControlPanel(false);
                myActionStatus = ActionStatus.EDITING;
            }
            else
            {
                MessageBoxEx.Show("Không có nhân viên nào được chọn", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Ấn button tìm kiếm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmSalary.btnSearch_Click", "", "");
            bool bIsFirstPay = false;
            if (myActionStatus == ActionStatus.DEFAULT)
            {
                pnlBottom.Visible = false;
                if (cmbFirstOrEnd.SelectedItem.ToString() == cmbItemStart.Text)
                    bIsFirstPay = true;
                else
                    bIsFirstPay = false;

                if (chkStaff.Checked == true)
                {
                    myBinding.DataSource = mySalaryCtrl.fsLoadSalaryList(cmbStaff.SelectedValue.ToString(), bIsFirstPay, dateTime.Value);
                }
                else
                {
                    myBinding.DataSource = mySalaryCtrl.fsLoadSalaryList(string.Empty, bIsFirstPay, dateTime.Value);
                }

                grdMain.DataSource = myBinding;

                if (grdMain.CurrentCell != null)
                {
                    this.fsBindingDataToBottomPanel(grdMain.CurrentCell.RowIndex);
                }

                this.fsCalcSumSalary();//Tính tổng số tiền
            }
        }

        /// <summary>
        /// Ấn button lưu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmSalary.btnSave_Click", "", "");
            if (myActionStatus != ActionStatus.DEFAULT)
            {
                if (mySalaryCtrl.fsUpdateSalary(txtSalaryCode.Text,
                                                txtSalaryChangeCode.Text,
                                                Convert.ToInt64(numSalaryChange.Value),
                                                txtNote.Text, chkIsPay.Checked,
                                                datePayDate.Value == DateTime.MinValue ? DateTime.MaxValue : datePayDate.Value,
                                                CommonUtils.fsGetOPID(), DateTime.Now) == ResultStatus.SUCCESS)
                {//Save thành công

                    //Chuyển trạng thái
                    this.myActionStatus = ActionStatus.DEFAULT;

                    //control ẩn hiện các panel
                    //pnlBottom.Visible = false;
                    //pnlCenter.Enabled = true;
                    //pnlTop.Enabled = true;
                    this.fsControlPanel(true);

                    //Load lại grid
                    btnSearch_Click(new object(), new EventArgs());
                }
                else
                {//Save thất bại
                    MessageBoxEx.Show(CommonMessages.MSG_SAVE_FAIL, CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        /// <summary>
        /// Ấn button hủy
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmSalary.btnCancel_Click", "", "");
            if (myActionStatus != ActionStatus.DEFAULT)
            {
                //Chuyển trạng thái
                myActionStatus = ActionStatus.DEFAULT;

                //Control hiển thị các panel
                //pnlBottom.Visible = false;
                //pnlTop.Enabled = true;
                //pnlCenter.Enabled = true;
                this.fsControlPanel(true);
            }
        }

        /// <summary>
        /// Ấn button thoát
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBack_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmSalary.btnBack_Click", "", "");
            this.Close();
        }

        /// <summary>
        /// Thay đổi trạng thái check nhân viên
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkStaff_CheckedChanged(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmSalary.chkStaff_CheckedChanged", "", "");
            if (chkStaff.Checked == true)
            {
                cmbStaff.Enabled = true;
            }
            else
            {
                cmbStaff.Enabled = false;
            }
        }

        /// <summary>
        /// Thay đổi trạng thái chkIsPay
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkIsPay_CheckedChanged(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmSalary.chkIsPay_CheckedChanged", "", "");
            if (chkIsPay.Checked == true)
            {
                lbPayStatus.Text = "<b>Đã trả</b>";
            }
            else
            {
                lbPayStatus.Text = "<b>Chưa trả</b>";
            }
        }

        /// <summary>
        /// Cell click trên grid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grdMain_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmSalary.grdMain_CellClick", "", "");
            this.fsBindingDataToBottomPanel(e.RowIndex);
        }

        /// <summary>
        /// Setting header.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void headerItem1_Load(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmSalary.headerItem1_Load", "", "");
            this.headerItem1.init("THÔNG TIN TIỀN LƯƠNG", false);
            //this.headerItem1.btnChangeStyle.Visible = false;
            //this.headerItem1.btnSwitchUser.Visible = false;
            //this.headerItem1.lblShift.Visible = false;
            //this.headerItem1.lblShift1.Visible = false;            
        }

        /// <summary>
        /// Khi đóng đang đóng
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmSalary_FormClosing(object sender, FormClosingEventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmSalary.frmSalary_FormClosing", "", "");
            if (myCloseForm != null)//Thông báo cho form menu
                myCloseForm();
        }

        #endregion

        #region Functions ***************************************

        /// <summary>
        /// Binding thông tin từ grid vào bottom panel
        /// </summary>
        /// <param name="pRowIndex"></param>
        private void fsBindingDataToBottomPanel(int pRowIndex)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmSalary.fsBindingDataToBottomPanel", "", "");
            cmbStaffBottom.SelectedValue = grdMain.Rows[pRowIndex].Cells["colSTAFF_CODE"].Value;
            txtNote.Text = grdMain.Rows[pRowIndex].Cells["colSALARY_CHANGE_NOTE"].Value != null ? grdMain.Rows[pRowIndex].Cells["colSALARY_CHANGE_NOTE"].Value.ToString() : string.Empty;
            chkIsPay.Checked = Convert.ToBoolean(grdMain.Rows[pRowIndex].Cells["colIS_PAID"].Value);
            if (Convert.ToDateTime(grdMain.Rows[pRowIndex].Cells["colPAY_DATE"].Value).ToShortDateString() == DateTime.MaxValue.ToShortDateString())
            {
                datePayDate.Value = DateTime.MinValue;
            }
            else
            {
                datePayDate.Value = Convert.ToDateTime(grdMain.Rows[pRowIndex].Cells["colPAY_DATE"].Value);
            }
            numSalaryChange.Value = Convert.ToInt64(grdMain.Rows[pRowIndex].Cells["colSALARY_CHANGE_AMOUNT"].Value);
            txtSalaryCode.Text = grdMain.Rows[pRowIndex].Cells["colSALARY_CODE"].Value.ToString();
            txtSalaryChangeCode.Text = grdMain.Rows[pRowIndex].Cells["colSALARY_CHANGE_CODE"].Value != null ? grdMain.Rows[pRowIndex].Cells["colSALARY_CHANGE_CODE"].Value.ToString() : string.Empty;
        }

        /// <summary>
        /// Control ẩn/hiện các panel
        /// </summary>
        /// <param name="pEnable"></param>
        private void fsControlPanel(bool pEnable)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmSalary.fsControlPanel", "", "");
            pnlBottom.Visible = !pEnable;//Ngược lại với các panel khác
            pnlTop.Enabled = pEnable;
            pnlCenter.Enabled = pEnable;
        }

        private void fsCalcSumSalary()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmSalary.fsCalcSumSalary", "", "");
            long nSumSalary = 0;

            foreach (var item in myBinding)
            {
                nSumSalary += Convert.ToInt64(((VIEW_Salary)item).iTOTAL_PAY_SALARY);
            }

            lbSumSalary.Text = nSumSalary.ToString("C0");
        }

        #endregion


    }
}